{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 247,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精准度76.14%\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "x = np.zeros((351,34),dtype = \"float\")\n",
    "y = np.zeros((351),dtype = \"byte\")\n",
    "\n",
    "# 数据文件名\n",
    "file_name = \"ionosphere.data\"\n",
    "\n",
    "reader = None\n",
    "with open(file_name,\"r\") as input_file:\n",
    "    reader = csv.reader(input_file)\n",
    "    for i,row in enumerate(reader):\n",
    "        datas = [float(data) for data in row[:-1]]\n",
    "        x[i] = datas\n",
    "        y[i] = row[-1] == 'g'\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test = train_test_split(x,y,random_state = 2)\n",
    "\n",
    "\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "estimator = KNeighborsClassifier()\n",
    "estimator.fit(x_train,y_train)\n",
    "\n",
    "y_predict = estimator.predict(x_test)\n",
    "accuracy = np.mean(y_predict == y_test) * 100\n",
    "print(\"精准度{0:.2f}%\".format(accuracy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均的精准度82.62%\n"
     ]
    }
   ],
   "source": [
    "# 进行交叉验证\n",
    "from sklearn.model_selection import cross_val_score\n",
    "\n",
    "scores = cross_val_score(estimator, x, y, scoring='accuracy') \n",
    "average_accuracy = np.mean(scores) * 100 \n",
    "print(\"平均的精准度{0:.2f}%\".format(average_accuracy)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: http://mirrors.aliyun.com/pypi/simple/\n",
      "Collecting matplotlib\n",
      "  Downloading http://mirrors.aliyun.com/pypi/packages/0c/cd/829b2795de5c6667133d39256a40deb7f4a12753872cf3f128f82d86d25c/matplotlib-3.2.0-cp38-cp38-manylinux1_x86_64.whl (12.4 MB)\n",
      "\u001b[K     |████████████████████████████████| 12.4 MB 2.5 MB/s eta 0:00:01████▍                      | 3.6 MB 2.5 MB/s eta 0:00:04\n",
      "\u001b[?25hCollecting kiwisolver>=1.0.1\n",
      "  Downloading http://mirrors.aliyun.com/pypi/packages/64/8b/a70681c9a471f8187fed80d0aa9c9bb55ec3bf9daa50bd1cdc0c73d4910c/kiwisolver-1.1.0-cp38-cp38-manylinux1_x86_64.whl (91 kB)\n",
      "\u001b[K     |████████████████████████████████| 91 kB 63.1 MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1\n",
      "  Downloading http://mirrors.aliyun.com/pypi/packages/5d/bc/1e58593167fade7b544bfe9502a26dc860940a79ab306e651e7f13be68c2/pyparsing-2.4.6-py2.py3-none-any.whl (67 kB)\n",
      "\u001b[K     |████████████████████████████████| 67 kB 50.6 MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: numpy>=1.11 in /root/anaconda3/lib/python3.8/site-packages (from matplotlib) (1.18.1)\n",
      "Collecting cycler>=0.10\n",
      "  Downloading http://mirrors.aliyun.com/pypi/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)\n",
      "Requirement already satisfied: python-dateutil>=2.1 in /root/anaconda3/lib/python3.8/site-packages (from matplotlib) (2.8.1)\n",
      "Requirement already satisfied: setuptools in /root/anaconda3/lib/python3.8/site-packages (from kiwisolver>=1.0.1->matplotlib) (45.2.0.post20200210)\n",
      "Requirement already satisfied: six in /root/anaconda3/lib/python3.8/site-packages (from cycler>=0.10->matplotlib) (1.14.0)\n",
      "Installing collected packages: kiwisolver, pyparsing, cycler, matplotlib\n",
      "Successfully installed cycler-0.10.0 kiwisolver-1.1.0 matplotlib-3.2.0 pyparsing-2.4.6\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存每一次的平均分数\n",
    "avg_scores = []\n",
    "# 遍历k的取值从1到25\n",
    "for i in range(1,25):\n",
    "    estimator = KNeighborsClassifier(i)\n",
    "    scores = cross_val_score(estimator, x, y, scoring='accuracy') * 100\n",
    "    avg_scores.append(np.mean(scores))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f2ff584c4f0>]"
      ]
     },
     "execution_count": 251,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXUAAAD4CAYAAAATpHZ6AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8GearUAAAgAElEQVR4nO2deXxU5bnHf08IW1iDJMomYFVQQJEZFK1Gg9QqGpeLqFhsXShxudraW9FaubT26m25WO9trSK2bnUrRdpmBAULwYkbJALKZhGLCZsQQFbZAs/948lphjDLWd7ZzjzfzyefyZw55z3vnHnnN+953mchZoaiKIriD/LS3QFFURTFHCrqiqIoPkJFXVEUxUeoqCuKovgIFXVFURQfkZ/Kk3Xt2pX79OmTylMqiqJkPR999NFWZi6ys29KRb1Pnz6oqalJ5SkVRVGyHiKqtbuvml8URVF8hIq6oiiKj1BRVxRF8REq6oqiKD5CRV1RFMVH+E/UJ08GKiuP3lZZKdsVRVF8jv9EfehQ4LrrmoS9slKeDx2a3n4piqKkgJT6qaeE0lJg+nTg2muB4cOBBQvkeWlpunumKIqSdPw3UwdEwAcMAGbMAG69VQVd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QUbvGxRAQUTsAlwBYHv8oX1MB4HuN/38PwN/S2Je0YYlYI9cgB8YESdm0PwBYxcy/jngpp8ZErOvgZkykOvXuSAD/C6AFgGeZ+ZGUnTyDIKKTILNzQCpIvZIr14KIXgVwESSl6GZIRa2/ApgO4ERIaubrmNnXi4gxrsNFkNtsBvAFgPIIu7IvIaLzAVQBWAbAqnX8IMSenDNjIs51GAOHY0LTBCiKovgIXShVFEXxESrqiqIoPkJFXVEUxUeoqCuKovgIFXVFURQfoaKuKIriI1TUFUVRfMT/A8sTDstGVr6nAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "x_len = list(range(1,len(avg_scores)+1))\n",
    "plt.plot(x_len,avg_scores, '-x',color = \"r\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均的精准度73.78%\n"
     ]
    }
   ],
   "source": [
    "x_broken = np.array(x)\n",
    "x_broken[:,::2] /= 10\n",
    "\n",
    "estimator = KNeighborsClassifier()\n",
    "scores = cross_val_score(estimator, x_broken, y, scoring='accuracy') \n",
    "average_accuracy = np.mean(scores) * 100 \n",
    "print(\"平均的精准度{0:.2f}%\".format(average_accuracy)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 280,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均的精准度82.90%\n"
     ]
    }
   ],
   "source": [
    "# 归一化\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "x_formed = MinMaxScaler().fit_transform(x_broken)\n",
    "\n",
    "estimator = KNeighborsClassifier()\n",
    "scores = cross_val_score(estimator, x_formed, y, scoring='accuracy') \n",
    "average_accuracy = np.mean(scores) * 100 \n",
    "print(\"平均的精准度{0:.2f}%\".format(average_accuracy)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Pipeline] ............... (step 1 of 2) Processing 预处理, total=   0.0s\n",
      "[Pipeline] ............... (step 2 of 2) Processing 估计器, total=   0.0s\n",
      "[Pipeline] ............... (step 1 of 2) Processing 预处理, total=   0.0s\n",
      "[Pipeline] ............... (step 2 of 2) Processing 估计器, total=   0.0s\n",
      "[Pipeline] ............... (step 1 of 2) Processing 预处理, total=   0.0s\n",
      "[Pipeline] ............... (step 2 of 2) Processing 估计器, total=   0.0s\n",
      "[Pipeline] ............... (step 1 of 2) Processing 预处理, total=   0.0s\n",
      "[Pipeline] ............... (step 2 of 2) Processing 估计器, total=   0.0s\n",
      "[Pipeline] ............... (step 1 of 2) Processing 预处理, total=   0.0s\n",
      "[Pipeline] ............... (step 2 of 2) Processing 估计器, total=   0.0s\n",
      "平均的精准度82.90%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.pipeline import Pipeline\n",
    "\n",
    "mining_pipe = Pipeline(\n",
    "[\n",
    "   ('预处理',MinMaxScaler()),\n",
    "    ('估计器',KNeighborsClassifier())\n",
    "]\n",
    ",verbose=True\n",
    ")\n",
    "\n",
    "scores = cross_val_score(mining_pipe, x_broken, y, scoring='accuracy') \n",
    "average_accuracy = np.mean(scores) * 100 \n",
    "print(\"平均的精准度{0:.2f}%\".format(average_accuracy)) "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
